package com.hnkywl.sts.service.cwgl;

import com.hnkywl.sts.dao.cwgl.HzdrDao;
import com.hnkywl.sts.dao.cwgl.SkdrDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by HL on 2015/9/24.
 */
@Service
public class HzdrService {
    private GsjgDao gsjgDao;
    private HzdrDao hzdrDao;


    @Transactional
    public String findAllHzdrdr(Date skshStart, Date skshEnd, String bm, String sbgsMc, Long sbgsId, String gldwMc, String zt, String skr, int pageNo, int pageSize) {
        Long nbgsIds = null;
        if (sbgsId != null && sbgsMc.equals(gsjgDao.get(sbgsId).getJc())) {
            nbgsIds = nbgsIds;
        }
        Page<Sfk> hzdrPage = this.hzdrDao.findHzdrdrList(skshStart, skshEnd, bm, sbgsMc, nbgsIds, gldwMc, zt, skr, pageNo, pageSize);
        List<Sfk> hzdrList = hzdrPage.getResult() == null ? new ArrayList<Sfk>() : hzdrPage.getResult();
        if(hzdrList.size()>0){
            hzdrList = getCzztByPj(hzdrList);
        }
        String hzdrJson = BaseUtils.fetchCollectionToJson(hzdrList, "id", "bm", "sfkrq", "sfklx", "gldw.qc", "kxyt.z", "kxsy.z", "fkfs.z", "sfje", "pjlx.z", "kxlx", "sbgs.jc", "sbr.xm", "bz", "zt", "shrq", "khfksj", "pjhm",
               "sfhz","acctyear", "uuid", "voucherId", "voucherNum", "insertFlag", "deleteFlag");
        return "{\"total\":" + hzdrPage.getTotalCount() + ",\"rows\":[" + hzdrJson + "]}";
    }

    @Transactional
    public Sfk findhzdrDetail(Long id) {
        return this.hzdrDao.getHzdr(id);
    }


    public List<Sfk> getCzztByPj(List<Sfk> sfkList) {
        for (Sfk p : sfkList) {
            String czzt = "";
            if (p.getInsertFlag() == 1 && p.getDeleteFlag() == 1) {
                czzt = "wdr";
            } else if (p.getInsertFlag() == 1 && p.getDeleteFlag() == 3) {
                czzt = "sccg";
            } else if (p.getInsertFlag() == 3 && p.getDeleteFlag() == 1) {
                czzt = "drcg";
            } else if (p.getInsertFlag() == 2 && p.getDeleteFlag() == 1 || p.getInsertFlag() == 2 && p.getDeleteFlag() == 3) {
                czzt = "drsb";
            } else if (p.getInsertFlag() == 3 && p.getDeleteFlag() == 2) {
                czzt = "scsb";
            } else {
                czzt = "null";
            }
            p.setCzzt(czzt);
        }
        return sfkList;
    }


    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
    @Autowired
    public void setHzdrDao(HzdrDao hzdrDao) {
        this.hzdrDao = hzdrDao;
    }
}
